package com.huangge1199.common.controller;

import com.alibaba.fastjson.JSON;
import com.huangge1199.common.ex.BaseException;
import com.huangge1199.common.util.ExcelDeal;
import com.huangge1199.common.util.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author: huang
 * @Description excel操作demo
 * @Date: 2021-01-02 09:05
 */
@RestController
@RequestMapping("/excel")
@Slf4j
public class ExcelController {

    @RequestMapping(value = "in",method = RequestMethod.POST)
    public Response<List<Map<String, String>>> excelIn(@RequestBody MultipartFile file){
        Response<List<Map<String, String>>> response;
        try {
            List<Map<String, String>> mapList = ExcelDeal.getInstance().excelIn(new ArrayList<>(),file);
            response = new Response<>(mapList);
        } catch (BaseException e) {
            log.debug(e.getMsg());
            response = new Response<>(e.getMsg());
        }
        return response;
    }

    @RequestMapping(value = "out",method = RequestMethod.POST)
    public void excelOut(@RequestBody String str, HttpServletResponse response){
        try {
            List<Map<String,String>> dataList = (List<Map<String,String>>) JSON.parse(str);
            ExcelDeal.getInstance().excelOut(dataList,response);
        } catch (IOException e) {
            log.debug(e.getMessage());
        }
    }
}
